﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Zyg.SSO.Core.Business;

namespace Zyg.SSO.Web.User
{
    public partial class RegisterUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Zyg.SSO.Core.Business.SysSetting settings = Zyg.SSO.Core.Business.SysSetting.GetCurrent();
            Copyright.Text = settings.Copyright;
        }

        protected void BtnSubmit_Click(object sender, EventArgs e)
        {
            Zyg.SSO.Core.Business.Account account = new Zyg.SSO.Core.Business.Account();
            Zyg.Security.SecurityHelper sh = new Zyg.Security.SecurityHelper();

            account.Id = Guid.NewGuid();
            account.Email = AccountEmail.Text;
            account.IsAdmin = false;
            account.Name = AccountName.Text;
            account.Password = sh.ComputeMD5Hash(AccountPwd.Text);
            account.PassQuestion = AccountPassQuestion.Text;
            account.PassAnswer = sh.ComputeMD5Hash(AccountPassAnswer.Text);
            account.RoleID = Zyg.SSO.Core.Business.Role.User.Id;
            account.State = AccountState.Register;
            account.Telephone = AccountTel.Text;
            account.Remark = AccountRemark.Text;

            if (FileAccountPic.HasFile)
            {
                string accountContentPath = Server.MapPath("~/Content/Account/");
                string extension = Zyg.Utility.Helper.FileHelper.GetFileExtension(FileAccountPic.FileName);

                string fileName = account.Id.ToString() + extension;

                account.PictureAddress = "~/Content/Account/" + fileName;

                FileAccountPic.SaveAs(accountContentPath + fileName);
            }

            account.Save();

            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "success",
                "<script type='text/javascript'>alert('申请注册成功,请等待管理员审核!现在将跳转到登录页面');window.location.href='../Login.aspx';</script>");
        }
    }
}
